home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * glyph.c:
- *
- * a simple example of changing a window's cursor using "Glyph" cursors.
- * A "Glyph" is just a character from one of the many text fonts that X
- * knows about. The size of your new glyph cursor is determined by the
- * pixel size in the font's name. This example should probably call
- * `XQueryBestCursor()' to find the "best" (or at least the biggest
- * usable) cursor size, but it simply arbitrarily chooses an Helvetica
- * font with a pixel size of 25. Note that some of the "character
- * positions" in the array defining the characters in a font may be
- * undefined, and among other things, a cursor must have non-zero width.
- * So, after you load the font you've chosen, you need to query the "text
- * extents" of each of the characters that might be defined in the font.
- * Also note that the first glyph cursor "displayed" by the program is
- * the space character which has a width, but doesn't display a very
- * interesting cursor.
- */
- #include <stdio.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- main()
- {
- Display *dpy;
- Window wind;
- unsigned int scr;
- unsigned int keep_going = 1;
- XEvent ev;
- unsigned int j, ichar, b1;
- unsigned int i;
- XColor backg, ebackg, foreg, eforeg, red, ered;
- Window rwin;
- Colormap dcm;
- GC dgc;
- const unsigned int MAX_glyphs=65535;
- struct {
- Cursor glyphc;
- unsigned char byte1;
- unsigned char byte2;
- } curs[MAX_glyphs];
- XFontStruct *fs;
- unsigned char id[256];
- static unsigned char drexions[] = "Mouse button to cycle thru Glyph Cursors.";
- static unsigned char fontname[]
- = "-adobe-helvetica-medium-r-normal--25-180-100-100-p-130-iso8859-1";
- unsigned int num_glyphs;
- XWMHints wmhints;
- XSizeHints wmsizehints;
- XClassHint classhint;
-
- dpy = XOpenDisplay("");
- scr = DefaultScreen(dpy);
- rwin = RootWindow(dpy, scr);
- dcm = DefaultColormap(dpy, scr);
- dgc = DefaultGC(dpy,scr);
-
- #if defined(DEBUGIT)
- XSynchronize(dpy,True);
- #endif
- if( !XAllocNamedColor(dpy, dcm, "darkslategray", &backg, &ebackg)) {
- fprintf( stderr, "Error getting darkslategray\n" ); exit(1); }
-
- if( !XAllocNamedColor(dpy, dcm,"burlywood", &foreg, &eforeg)) {
- fprintf( stderr, "Error getting burlywood\n" ); exit(1); }
-
- if( !XAllocNamedColor(dpy, dcm,"red", &red, &ered)) {
- fprintf( stderr, "Error getting red\n" ); exit(1); }
-
- fs = XLoadQueryFont( dpy, fontname );
- if( fs == NULL ) {
- fprintf(stderr, "Couldn't find font %s\n", fontname);
- exit(1);
- }
-
- #if defined(TRACEIT)
- printf("Font Properties (names):\n");
- for( i = 0; i < fs->n_properties; i++ ) {
- printf("%s\n", XGetAtomName(dpy, fs->properties[i].name));
- }
- #endif
-
- for( num_glyphs = 0, b1 = fs->min_byte1; b1 <= fs->max_byte1; b1++ ) {
- for( ichar = 0;
- (num_glyphs < MAX_glyphs) && (ichar <= fs->max_char_or_byte2);
- ichar++ ) {
- XChar2b thechar;
- int direction, fontascent, fontdescent;
- XCharStruct metrics;
- #if defined(TRACEIT)
- printf("%d\n", ichar);
- #endif
-
- if( (fs->min_byte1 == 0) && (fs->max_byte1 == 0) ) {
- thechar.byte1 = (ichar >> 8); /* high eight bits */
- thechar.byte2 = (ichar & 255); /* low eight bits */
- } else {
- thechar.byte1 = (b1 & 255);
- thechar.byte2 = (ichar & 255); /* low eight bits */
- }
-
- XTextExtents16 (fs, &thechar, 1, &direction, &fontascent, &fontdescent,
- &metrics);
- #if defined(TRACEIT)
- printf( "char %d has width %d\n", ichar, metrics.width );
- #endif
- if( metrics.width > 0 ) {
- curs[num_glyphs].glyphc = XCreateGlyphCursor(dpy, fs->fid, fs->fid,
- ichar, ichar,
- &foreg, &red );
- curs[num_glyphs].byte1 = b1;
- curs[num_glyphs].byte2 = ichar;
- if( curs[num_glyphs].glyphc == NULL ) {
- fprintf( stderr, "Error creating cursor from font\n" );
- exit(1);
- }
- num_glyphs += 1;
- }
- }
- }
-
- wind = XCreateSimpleWindow( dpy, rwin, 0, 0, 512, 512, 2,
- foreg.pixel, backg.pixel);
- classhint.res_name = "LEFT or RIGHT Mouse to Change Cursor";
- classhint.res_class = "GlyphCursor";
- XSetClassHint(dpy, wind, &classhint);
-
- wmhints.input = True;
- wmhints.flags = InputHint;
- XSetWMHints(dpy, wind, &wmhints);
-
- wmsizehints.x = 100;
- wmsizehints.y = 100;
- wmsizehints.width = 100;
- wmsizehints.height = 100;
- wmsizehints.flags = USPosition | USSize;
- XSetWMNormalHints(dpy, wind, &wmsizehints);
-
- XSetWindowBackground( dpy, wind, backg.pixel );
- XSetBackground( dpy, dgc, backg.pixel );
- XSetForeground( dpy, dgc, foreg.pixel );
-
- XSelectInput( dpy, wind, ButtonPressMask|KeyPressMask|ExposureMask);
- XMapWindow( dpy, wind );
-
- i = 0;
- do {
- XNextEvent( dpy, &ev );
- switch( ev.type )
- {
- case Expose:
- while( XCheckTypedEvent(dpy, Expose, &ev) );
- XUndefineCursor( dpy, wind );
- XClearWindow(dpy, wind);
- XDefineCursor( dpy, wind, curs[i%num_glyphs].glyphc );
- sprintf(id, "(%u, %u)", curs[i%num_glyphs].byte1, curs[i%num_glyphs].byte2);
- XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
- XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
- break;
-
- case KeyPress:
- XCloseDisplay(dpy);
- keep_going = 0;
- break;
-
- case ButtonPress:
- if( ev.xbutton.button == Button1 )
- i += 1;
- else if( ev.xbutton.button == Button3 )
- i -= 1;
- else
- i = 0;
- XUndefineCursor( dpy, wind );
- XClearWindow(dpy, wind);
- XDefineCursor( dpy, wind, curs[i%num_glyphs].glyphc );
- sprintf(id, "(%u, %u)", curs[i%num_glyphs].byte1, curs[i%num_glyphs].byte2);
- XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
- XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
- break;
-
- default:
- break;
- }
- } while( keep_going );
- exit(0);
- }
-